﻿using System.Web.Mvc;
using System.Collections.Generic;
using System.Linq;
using System.Data;
using System.Reflection;
using Kooboo.Web.Areas.Commerce.Binders;

using Kooboo.Web.Areas.Commerce.Web;
using System.ComponentModel.Composition;
using System.Web.Routing;
using Kooboo.Commerce;

namespace Kooboo.Web.Areas.Commerce
{
    [Export(typeof(AreaRegistration))]
    public class CommerceAreaRegistration : AreaRegistration
    {
        public override string AreaName
        {
            get
            {
                return "Commerce";
            }
        }

        public override void RegisterArea(AreaRegistrationContext context)
        {

            #region brand
            context.MapRouteEx(
                "commerce/brands",
                "commerce/products/brands",
                new { controller = "Brand", action = "Index", AreaName = this.AreaName },
                "/commerce/products/brands");

            context.MapRouteEx(
               "commerce/brands/create",
               "commerce/products/brands/create",
               new { controller = "Brand", action = "Create", AreaName = this.AreaName },
               "/commerce/products/brands/create");

            context.MapRouteEx(
                "commerce/brands/edit",
                "commerce/products/brands/edit/{id}",
                new { controller = "Brand", action = "Edit", id = UrlParameter.Optional, AreaName = this.AreaName },
                "/commerce/products/brands/edit");

            context.MapRouteEx(
                "commerce/brands/delete",
                "commerce/products/brands/delete",
                new { controller = "Brand", action = "Delete", AreaName = this.AreaName },
                "/commerce/products/brands/delete");
            //TODO:highlight mentu item for dynamic url
            context.MapRoute(
                "commerce/brand/products",
                "commerce/products/brands/{id}/products",
                new { controller = "Brand", action = "Products" });

            #endregion

            var child_category_products = context.MapRoute(
                "commerce_category_products-parents",
                "commerce/products/categories/{parents}-{id}/products",
                new { controller = "Category", action = "Products" }
                );

            var category_product = context.MapRoute(
                  "commerce_category_products",
                  "commerce/products/categories/{id}/products",
                  new { controller = "Category", action = "Products" }
              );

            var category = context.MapRoute(
                "commerce_categories",
                "commerce/products/categories/{action}/{id}",
                new { controller = "Category", action = "Index", id = UrlParameter.Optional, AreaName = this.AreaName });

            var product = context.MapRoute(
                "commerce_products",
                "commerce/products/{action}/{id}",
                new { controller = "Product", action = "Index", id = UrlParameter.Optional, AreaName = this.AreaName });

            #region order

            context.MapRouteEx(
                "commerce/orders",
                "commerce/orders",
                new { controller = "Order", action = "Index", AreaName = this.AreaName },
                "/commerce/orders");

            context.MapRouteEx(
                "commerce/orders/create",
                "commerce/orders/create",
                new { controller = "Order", action = "Create", AreaName = this.AreaName },
                "/commerce/orders/create");

            context.MapRouteEx(
                "commerce/orders/detail",
                "commerce/orders/detail/{Id}",
                new { controller = "Order", action = "Detail", AreaName = this.AreaName },
                "/commerce/orders/detail");


            context.MapRouteEx(
                "commerce/orders/searchproducts",
                "commerce/orders/searchproducts",
                new { controller = "Order", action = "SearchProducts", AreaName = this.AreaName },
                "/commerce/orders/searchproducts");


            context.MapRouteEx(
                "commerce/orders/searchcustomers",
                "commerce/orders/searchcustomers",
                new { controller = "Order", action = "SearchCustomers", AreaName = this.AreaName },
                "/commerce/orders/searchcustomers");


            context.MapRouteEx(
                "commerce/orders/calculate",
                "commerce/orders/calculate",
                new { controller = "Order", action = "Calculate", AreaName = this.AreaName },
                "/commerce/orders/calculate");


            context.MapRouteEx(
                "commerce/orders/changestatus",
                "commerce/orders/changestatus",
                new { controller = "Order", action = "ChangeStatus", AreaName = this.AreaName },
                "/commerce/orders/changestatus");

            context.MapRouteEx(
               "commerce/orders/updateremark",
               "commerce/orders/updateremark",
               new { controller = "Order", action = "UpdateRemark", AreaName = this.AreaName },
               "/commerce/orders/updateremark");

            #endregion

            #region customer

            context.MapRouteEx(
                "commerce/customers",
                "commerce/customers/{page}",
                new { controller = "Customer", action = "Index", AreaName = this.AreaName, page = UrlParameter.Optional },
                new { page = @"^\d*$" },
                "/commerce/customers");

            context.MapRouteEx(
                "commerce/customers/create",
                "commerce/customers/create",
                new { controller = "Customer", action = "Create", AreaName = this.AreaName },
                "/commerce/customers/create");

            context.MapRouteEx(
                "commerce/customers/edit",
                "commerce/customers/edit/{id}",
                new { controller = "Customer", action = "Edit", id = UrlParameter.Optional, AreaName = this.AreaName },
                "/commerce/customers/edit");


            #region Addresses
            context.MapRouteEx(
                "commerce/customers/addresses",
                "commerce/customers/addresses",
                new { controller = "Customer", action = "Addresses", AreaName = this.AreaName },
                "/commerce/customers/addresses");

            context.MapRouteEx(
                "commerce/customers/addaddress",
                "commerce/customers/addaddress",
                new { controller = "Customer", action = "AddAddress", AreaName = this.AreaName },
                "/commerce/customers/addaddress");


            context.MapRouteEx(
            "commerce/customers/updateaddress",
            "commerce/customers/updateaddress",
            new { controller = "Customer", action = "UpdateAddress", AreaName = this.AreaName },
            "/commerce/customers/updateaddress");


            context.MapRouteEx(
            "commerce/customers/removeaddress",
            "commerce/customers/removeaddress",
            new { controller = "Customer", action = "RemoveAddress", AreaName = this.AreaName },
            "/commerce/customers/removeaddress");
            #endregion

            context.MapRouteEx(
                "commerce/customers/orders",
                "commerce/customers/orders",
                new { controller = "Customer", action = "Orders", AreaName = this.AreaName },
                "/commerce/customers/orders");

            context.MapRouteEx(
               "commerce/customers/changepassword",
               "commerce/customers/changepassword",
               new { controller = "Customer", action = "ChangePassword", AreaName = this.AreaName },
               "/commerce/customers/changepassword");

            #endregion

            #region Payment

            context.MapRouteEx(
                "commerce/shop/payments",
                "commerce/shop/payments",
                new { controller = "Payment", action = "Index", id = UrlParameter.Optional, AreaName = this.AreaName },
                "/commerce/shop/payments");

            context.MapRouteEx(
                "commerce/shop/payments/edit",
                "commerce/shop/payments/edit/{name}",
                new { controller = "Payment", action = "Edit", AreaName = this.AreaName },
                "/commerce/shop/payments/edit");

            context.MapRouteEx(
                "commerce/shop/payments/settings",
                "commerce/shop/payments/settings/{name}",
                new { controller = "Payment", action = "Settings", AreaName = this.AreaName },
                "/commerce/shop/payments/settings");

            context.MapRouteEx(
               "commerce/shop/payments/sync",
               "commerce/shop/payments/sync",
               new { controller = "Payment", action = "Sync", AreaName = this.AreaName },
               "/commerce/shop/payments/sync");
            #endregion

            #region shipping
            var shipping = context.MapRoute(
                "commerce/shop/shippings",
                "commerce/shop/shippings",
                new { controller = "Shipping", action = "Index", AreaName = this.AreaName });

            var shipping_create = context.MapRoute(
                "commerce/shop/shippings/create",
                "commerce/shop/shippings/create",
                new { controller = "Shipping", action = "Create", AreaName = this.AreaName });

            var shipping_edit = context.MapRoute(
                "commerce/shop/shippings/edit",
                "commerce/shop/shippings/edit",
                new { controller = "Shipping", action = "Edit", AreaName = this.AreaName });

            var shipping_delete = context.MapRoute(
                "commerce/shop/shippings/delete",
                "commerce/shop/shippings/delete",
                new { controller = "Shipping", action = "Delete", AreaName = this.AreaName });

            #endregion

            #region tax

            context.MapRouteEx(
                "commerce/shop/taxes",
                "commerce/shop/taxes",
                new { controller = "Tax", action = "Index", AreaName = this.AreaName },
                "/commerce/shop/taxes");

            context.MapRouteEx(
                "commerce/shop/taxes/create",
                "commerce/shop/taxes/create",
                new { controller = "Tax", action = "Create", AreaName = this.AreaName },
                "/commerce/shop/taxes/create");

            context.MapRouteEx(
                "commerce/shop/taxes/edit",
                "commerce/shop/taxes/edit",
                new { controller = "Tax", action = "Edit", AreaName = this.AreaName },
                "/commerce/shop/taxes/edit");

            context.MapRouteEx(
                "commerce/shop/taxes/delete",
                "commerce/shop/taxes/delete",
                new { controller = "Tax", action = "Delete", AreaName = this.AreaName },
                "/commerce/shop/taxes/delete");

            #endregion

            #region SupportTicket
            context.MapRouteEx(
               "commerce/services/supports",
               "commerce/services/supports",
                new { controller = "SupportTicket", action = "Index", AreaName = this.AreaName },
                "/commerce/services/supports");

            context.MapRouteEx(
                "commerce/services/supports/edit",
                "commerce/services/supports/edit/{id}",
                new { controller = "SupportTicket", action = "Edit", AreaName = this.AreaName },
                "/commerce/services/supports/edit");

            context.MapRouteEx(
                "commerce/services/supports/changstatus",
                "commerce/services/supports/changstatus/{id}",
                new { controller = "SupportTicket", action = "ChangeStatus", AreaName = this.AreaName },
                "/commerce/services/supports/changstatus");

            context.MapRouteEx(
                "commerce/services/supports/addreply",
                "commerce/services/supports/addreply/{supportId}",
                new { controller = "SupportTicket", action = "AddReply", AreaName = this.AreaName },
                "/commerce/services/supports/addreply");

            context.MapRouteEx(
                "commerce/services/supports/delete",
                "commerce/services/supports/delete",
                new { controller = "SupportTicket", action = "Delete", AreaName = this.AreaName },
                "/commerce/services/supports/delete");

            #endregion

            #region question
            context.MapRouteEx(
                "commerce/services/questions",
                "commerce/services/questions",
                new { controller = "ProductQuestion", action = "Index", AreaName = this.AreaName },
                "/commerce/services/questions");

            context.MapRouteEx(
               "commerce/services/questions/answer",
               "commerce/services/questions/answer",
               new { controller = "ProductQuestion", action = "Answer", AreaName = this.AreaName },
               "/commerce/services/questions/answer");

            context.MapRouteEx(
                "commerce/services/questions/delete",
                "commerce/services/questions/delete",
                new { controller = "ProductQuestion", action = "Delete", AreaName = this.AreaName },
                "/commerce/services/questions/delete");
            #endregion

            #region ProductReview

            context.MapRouteEx(
                "commerce/services/reviews",
                "commerce/services/reviews",
                new { controller = "ProductReview", action = "Index", AreaName = this.AreaName },
                "/commerce/services/reviews");

            context.MapRouteEx(
             "commerce/services/reviews/delete",
             "commerce/services/reviews/delete",
             new { controller = "ProductReview", action = "Delete", AreaName = this.AreaName },
             "/commerce/services/reviews/delete");

            context.MapRouteEx(
             "commerce/services/reviews/publish",
             "commerce/services/reviews/publish",
             new { controller = "ProductReview", action = "Publish", AreaName = this.AreaName },
             "/commerce/services/reviews/publish");


            #endregion

            #region ProductType
            context.MapRouteEx(
               "commerce/settings/producttypes",
               "commerce/settings/producttypes",
               new { controller = "ProductType", action = "Index", AreaName = this.AreaName },
               "/commerce/settings/producttypes");

            context.MapRouteEx(
                "commerce/settings/producttypes/delete",
                "commerce/settings/producttypes/delete",
                new { controller = "ProductType", action = "Delete", AreaName = this.AreaName },
                "/commerce/settings/producttypes/delete");

            context.MapRouteEx(
                "commerce/settings/producttypes/action",
                "commerce/settings/producttypes/{action}/{id}",
                new { controller = "ProductType", action = "Index", id = UrlParameter.Optional, AreaName = this.AreaName },
                "/commerce/settings/producttypes/action");


            #endregion

            var setting = context.MapRoute(
                "commerce/settings",
                "commerce/settings/{action}/{id}",
                new { controller = "Settings", action = "Index", id = UrlParameter.Optional, AreaName = this.AreaName });

            var account = context.MapRoute(
                "commerce/accounts",
                "commerce/accounts/{action}/{id}",
                new { controller = "Account", action = "Index", id = UrlParameter.Optional, AreaName = this.AreaName });


            #region promotion

            #region PromotionRule
            var promotionrule = context.MapRouteEx(
                   "commerce/shop/promotions",
                   "commerce/shop/promotions",
                   new { controller = "PromotionRule", action = "Index", AreaName = this.AreaName },
                   "/commerce/shop/promotions");

            var promotionrule_add = context.MapRouteEx(
                "commerce/shop/promotions/add",
                "commerce/shop/promotions/add",
                new { controller = "PromotionRule", action = "Create", AreaName = this.AreaName },
                "/commerce/shop/promotions/add");

            var promotionrule_delete = context.MapRouteEx(
              "commerce/shop/promotions/delete",
              "commerce/shop/promotions/delete",
              new { controller = "PromotionRule", action = "Delete", AreaName = this.AreaName },
              "/commerce/shop/promotions");

            var promotionrule_edit = context.MapRouteEx(
                "commerce/shop/promotions/edit",
                "commerce/shop/promotions/edit/{id}",
                new { controller = "PromotionRule", id = UrlParameter.Optional, action = "Edit", AreaName = this.AreaName },
                "/commerce/shop/promotions");

            var promotionrule_settings = context.MapRouteEx(
                "commerce/shop/promotions/settings",
                "commerce/shop/promotions/settings/{id}",
                new { controller = "PromotionRule", action = "Settings", AreaName = this.AreaName },
                "/commerce/shop/promotions/settings");


            var promotionrule_update = context.MapRouteEx(
                "commerce/shop/promotions/update",
                "commerce/shop/promotions/update",
                new { controller = "PromotionRule", action = "Update", AreaName = this.AreaName },
                "/commerce/shop/promotions/update");

            #endregion

            var promotion_addin = context.MapRouteEx(
                 "commerce/shop/promotions/extensions",
                 "commerce/shop/promotions/extensions",
                 new { controller = "Promotion", action = "Index", AreaName = this.AreaName },
                 "/commerce/shop/promotions/extensions");


            var promotion_addin_edit = context.MapRouteEx("commerce/shop/promotions/extensions/edit",
                "commerce/shop/promotions/extensions/edit/{id}",
                new { controller = "Promotion", action = "Edit", id = UrlParameter.Optional, AreaName = this.AreaName },
                "/commerce/shop/promotions/extensions/edit");



            #endregion

            var image_upload = context.MapRoute(
               "commerce_image",
               "commerce/image/upload",
               new { controller = "Image", action = "Upload" });

            #region Report
            /*
            var report = context.MapRoute("commerce_report_index",
                  "commerce/reports",
                  new { controller = "Report", action = "Order" });

            var report_order = context.MapRoute("commerce_report_order",
                 "commerce/reports/order",
                 new { controller = "Report", action = "Order" });

            var report_customer = context.MapRoute("commerce_report_customer",
                "commerce/reports/customer",
                new { controller = "Report", action = "Customer" });
             */
            #endregion

            var commerce_home = context.MapRoute(
                 "Commerce_home",
                 "commerce/home/{action}",
                 new { controller = "Home", action = "Index", id = UrlParameter.Optional });

            var commerce_default = context.MapRoute(
                 "Commerce_default",
                 "{commerce}",
                 new { controller = "Home", action = "Index" }, new { commerce = "^(commerce|admin)$" });

            RegisterCustomViewEngine();

            RegisterAllModelBinders();
        }

        private static void RegisterCustomViewEngine()
        {
            foreach (var viewEngine in ViewEngines.Engines.OfType<WebFormViewEngine>())
            {
                var masterLocationFormats = viewEngine.MasterLocationFormats.ToList();
                masterLocationFormats.Add("~/Areas/Commerce/Payments/{1}/{0}.master");
                masterLocationFormats.Add("~/Areas/Commerce/Promotions/{1}/{0}.master");


                viewEngine.MasterLocationFormats = masterLocationFormats.ToArray();

                var viewLocationFormats = viewEngine.ViewLocationFormats.ToList();
                viewLocationFormats.Add("~/Areas/Commerce/Payments/{1}/{0}.aspx");
                viewLocationFormats.Add("~/Areas/Commerce/Payments/{1}/{0}.ascx");

                viewLocationFormats.Add("~/Areas/Commerce/Promotions/{1}/{0}.aspx");
                viewLocationFormats.Add("~/Areas/Commerce/Promotions/{1}/{0}.ascx");


                viewEngine.ViewLocationFormats = viewLocationFormats.ToArray();



                viewEngine.PartialViewLocationFormats = viewEngine.ViewLocationFormats;
                viewEngine.AreaPartialViewLocationFormats = viewEngine.AreaViewLocationFormats;
            }
        }

        private static void RegisterAllModelBinders()
        {
            ModelBinders.Binders.DefaultBinder = new EntityBinder();
            ModelBinders.Binders.Add(typeof(IProductType), new ProductTypeBinder());
            ModelBinders.Binders.Add(typeof(IProduct), new ProductBinder());
            ModelBinders.Binders.Add(typeof(ITax), new TaxBinder());
            ModelBinders.Binders.Add(typeof(IShipping), new ShippingBinder());
            ModelBinders.Binders.Add(typeof(ICustomer), new CustomerBinder());

            ModelBinders.Binders.Add(typeof(IBrand), new BrandBinder());
            ModelBinders.Binders.Add(typeof(IShopSetting), new ShopSettingBinder());
            ModelBinders.Binders.Add(typeof(IOrder), new OrderBinder());
        }
    }
}
